package ase.assignment.sts.chart;

import java.rmi.RemoteException;

import org.eclipse.birt.chart.factory.IDataRowExpressionEvaluator;
import org.eclipse.birt.chart.integrate.SimpleDataRowExpressionEvaluator;

import ase.assignment.sts.api.CommonHandler;
import ase.assignment.sts.api.HandlerFactoryProxy;
import ase.assignment.sts.beans.PortfolioEntity;
import ase.assignment.sts.beans.StockAssertEntity;

public class ChartHelper {

	public static IDataRowExpressionEvaluator getPortfolios(String username) {
		String[] set = {
				"row[\"port_name\"]", "row[\"port_id\"]", "row[\"amount\"]", };//$NON-NLS-1$ //$NON-NLS-2$
		String[] names = null;
		Double[] amounts = null;
		Integer[] ids = null;
		try {
			PortfolioEntity[] ports = HandlerFactoryProxy.instance()
					.getPortfolioHandler().getAllPortfolios(username);
			names = new String[ports.length];
			amounts = new Double[ports.length];
			ids = new Integer[ports.length];
			for (int i = 0; i < ports.length; i++) {
				names[i] = ports[i].getName();
				ids[i] = ports[i].getId();
				amounts[i] = CommonHandler
						.getTotalOfPortfolio(ports[i].getId());
			}
		} catch (RemoteException e) {
			e.printStackTrace();
			return null;
		}
		Object[][] data = { names, ids, amounts };
		return new SimpleDataRowExpressionEvaluator(set, data);
	}

	public static IDataRowExpressionEvaluator getStocks(String portID) {
		String[] set = { "row[\"Category\"]", "row[\"Value\"]", };//$NON-NLS-1$ //$NON-NLS-2$
		String[] categorys = null;
		Double[] values = null;
		try {
			StockAssertEntity[] asserts = HandlerFactoryProxy.instance()
					.getStockHandler().getAllStocks(Integer.parseInt(portID));
			categorys = new String[asserts.length];
			values = new Double[asserts.length];
			for (int i = 0; i < asserts.length; i++) {
				categorys[i] = asserts[i].getName();
				values[i] = CommonHandler.getAmountOfStock(asserts[i].getId());
			}
		} catch (RemoteException e) {
			e.printStackTrace();
			return null;
		}
		Object[][] data = { categorys, values };
		return new SimpleDataRowExpressionEvaluator(set, data);
	}
}
